home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / disk / testandset.S < prev    next >
Encoding:
Text File  |  1996-05-05  |  3.1 KB  |  127 lines

  1. /*
  2.  * unsigned
  3.  * testandset(addr, value)
  4.  *        unsigned char *addr;
  5.  *
  6.  * testandset() returns '0' if it could set the byte lock to 
  7.  * a non-zero 'value', which may be ignored depending upon
  8.  * the implementation.  Otherwise, testandset() returns
  9.  * the current value of the  byte lock.
  10.  *
  11.  * Depending upon a particular 'value' for the byte may be
  12.  * a pitfall of using this function.  Also, assuming that
  13.  * the syncronization unit is a byte may be invalid also.
  14.  *
  15.  * Author: Joe Burger (bolo@cs.wisc.edu) on 28 November 1990
  16.  */
  17.  
  18. #if defined(sparc)
  19. /*
  20.  * The 'value' argument is ignored with the sparc implementation,
  21.  * although it could be implemented by writing the 'value' to the
  22.  * byte after ldstub "locks" it.
  23.  *
  24.  * Function of 'ldstub':
  25.  * Ldstub fetches the contents of the byte pointed to by 'addr'
  26.  * and replaces the contents of the byte with hex 0xff (decimal 255).
  27.  * Presumably this is done with a RWM bus cycle.
  28.  * The 'C' function return value is the previous value of the byte.
  29.  *
  30.  */
  31. #define    PROC(x)    .seg "text" ; .proc 14 ; .global x ; x :
  32.  
  33. PROC(_testandset)
  34.     retl
  35.     ldstub    [%o0],%o0    /* in delay slot */
  36. #endif
  37.  
  38. #if defined(mc68000) || defined(mc68010) || defined(mc68020) || defined(mc68030)
  39.  
  40. /*
  41.  * cas: compare and swap with operand; usage:  cas  dc,du,<ea>
  42.  * The 'dc' "compare" operand is compared to the value of the operand
  43.  * at <ea>.  If they are equal, the value at <ea> is replaced/updated
  44.  * with the value of the 'du' "update" operand.
  45.  */
  46. #define    PROC(x)    .text ; .even ; .globl x; x :
  47.  
  48. PROC(_testandset)
  49.     movl    sp@(4),a0    /* address */
  50.     movl    sp@(8),d1    /* value */
  51.     clrl    d0        /* compare operand for CAS */
  52.     casb    d0,d1,a0@
  53.     /* return is in d0 == old value of <ea> */
  54.     rts
  55. #endif
  56.  
  57. #if defined(vax)
  58.  
  59. /*
  60.  * bbssi: branch on bit set and set interlocked
  61.  * The bit is checked for its current value, then is always set.
  62.  * If the bit is set, the branch is taken, else execution falls through
  63.  */
  64. #define    PROC(x)    .text ; .align 1 ; .globl x; x :
  65.  
  66. PROC(_testandset)
  67.     .word    0x00    /* don't save any registers */
  68.     bbssi    $0,*4(ap),1f
  69.     /* bit not set, we "have" the byte */
  70.     clrl    r0    /* return value */
  71.     ret
  72. 1:    /* bit was set, we can't have it yet */
  73.     movl    $1,r0    /* return value */
  74.     ret
  75. #endif
  76.  
  77. #if defined(i386) || defined(linux)
  78. /*
  79.  * bts: bit test and set
  80.  * The bit is copied into the carry flag (CF) and then set bit
  81.  * xchg: exchange memory and register
  82.  * exchange contents of memory location and register
  83.  *
  84.  * currently ignores flag value
  85.  */
  86.  
  87. #define    PROC(x)    .text ; .align 2 ; .globl x ; x:
  88.  
  89. #if defined(SYSV)
  90. PROC(testandset)
  91. #elif defined(linux)
  92.     .text
  93.     .align    2
  94.     .globl    testandset
  95. testandset:
  96. #else
  97. PROC(_testandset)
  98. #endif
  99.     pushl    %ebp    /* build stack frame for debuggers */
  100.     movl    %esp,%ebp
  101.     movl    8(%ebp),%ecx    /* addr -> cx */
  102.     movl    $1,%eax        /* "1" == test and set grabbed */
  103.     lock
  104.     xchgb  (%ecx),%al    /* return value in %eax */
  105.     leave
  106.     ret
  107. #endif
  108.  
  109. #ifdef mips
  110. /*
  111.  * mips doesn't have a test-and-set instruction.
  112.  * this placeholder is here so the mips assembler doesn't choke
  113.  * on the empty input file
  114.  */
  115.     .align    2
  116. foooo:    .align    2
  117. #endif
  118.  
  119. #ifdef hpux
  120. /*
  121.  * We also do not have HP-UX test and set code.
  122.  */
  123.     .SPACE $PRIVATE$
  124.  
  125. #endif
  126.  
  127.